$(function () {
    var $datagrid = $("#datagrid"), $toolbar = $("#toolbar"), $dialog = $("#dialog");
    $("#save").on("click", function () {
        openDialog();
    });
    $datagrid.datagrid({
        singleSelect: true,
        fit: true,
        border: false,
        fitColumns: true,
        url: Constant.ctx("/mulanweb/admin/extension_personnel/search"),
        method: 'post',
        pagination: true,
        toolbar: $toolbar,
        columns: [[
            {field: 'Id', title: '编号'},
            {field: 'Name', title: '名称'},
            {field: 'Phone', title: '手机号'},
            {field: 'Remarks', title: '备注'},
            {field: 'AdminName', title: '创建者'},
            {field: 'CreateTime', title: '注册时间'},
        ]],
        onBeforeLoad: function (param) {
            param.PageNumber = param.page;
            param.PageSize = param.rows;

            return true;
        },
        loadFilter: function (data) {
            var newData = {};
            if (data.ReturnCode == 1) {
                newData.rows = data.DataList;
                if (newData.rows.length > 0) {
                    newData.total = data.TotalRow;
                } else {
                    newData.total = 0;
                }
            }
            return newData;
        }
    });
    function saveAccount() {
        $("form").form("submit", {
            url: $("form").attr("action"),
            onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// hide progress bar while the form is invalid
                }
                return isValid;
            },
            success: function (respText) {
                var resp = $.parseJSON(respText);
                if (resp.ReturnCode == 1) {
                    $("#datagrid").datagrid("reload");
                    $dialog.dialog("close");
                } else{
                   Constant.showMessage(resp.AlertMessage,respText.AlertMessage);
                }
            }
        });
    }

    function openDialog() {
        $dialog.dialog({
            title: "新增",
            width: 300,
            height: 400,
            closed: false,
            cache: false,
            href: 'form.jsp',
            modal: true,
            onClose: function () {
                $("#form-dialog").dialog("destroy");
            },
            buttons: [{
                text: '保存',
                handler: saveAccount
            }]
        });
    }
})

function doSearch() {
    $("#datagrid").datagrid("reload");
}